package com.quanyan.user.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.http.HttpClientUtils;
import com.quanyan.common.utils.GsonUtils;
import com.quanyan.user.service.UserSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户短信发送模块
 * Created by zhang on 16/11/15.
 */
@Service
public class UserSmsServiceImpl implements UserSmsService {

    private Logger logger = LoggerFactory.getLogger(UserSmsServiceImpl.class);

    @Value("${c.sendSmsApi}")
    private String sendSmsApi;

    @Value("${c.checkCodeApi}")
    private String checkCodeApi;

    @Value("${c.address}")
    private String address;

    /**
     * 短信下发
     * @param mobiles
     * @param templateId
     * @param type
     * @param params
     * @return
     */
    @Override
    public APIResponse sendSms(List<String> mobiles, int templateId, int type, List<String> params) {
        Map headMap = new HashMap<>();
        headMap.put("userCookiesName","");
        Map paramMap = new HashMap();
        paramMap.put("ver","");
        paramMap.put("protocol_ver",1);
        paramMap.put("mobiles",mobiles);
        paramMap.put("template_id",templateId);
        paramMap.put("type",type);
        paramMap.put("params",params);
        APIResponse response = null;
        String result = null;
        try {
            result = HttpClientUtils.postJson(address + sendSmsApi, GsonUtils.toJson(paramMap), headMap);
            logger.info("发送短信的手机号为：{}；返回结果为：{}",mobiles.toString(),result);
            response = GsonUtils.jsonStrToEntity(result, APIResponse.class);
        } catch (Exception e) {
            logger.info("请求返回信息：{}，异常信息：{}", result,e);
        }
        return response;
    }

    /**
     * 验证码校验
     * @param validCode 验证码
     * @return
     */
    @Override
    public APIResponse checkCode(String validCode,String mobile) {
        Map headMap = new HashMap<>();
        headMap.put("userCookiesName","");
        Map paramMap = new HashMap();
        paramMap.put("ver","");
        paramMap.put("protocol_ver",1);
        paramMap.put("verifycode",validCode);
        paramMap.put("mobile",mobile);
        APIResponse response = null;
        String result = null;
        try {
            result = HttpClientUtils.postJson(address + checkCodeApi, GsonUtils.toJson(paramMap), headMap);
            logger.info("验证码校验返回结果：{}",result);
            response = GsonUtils.jsonStrToEntity(result, APIResponse.class);
        } catch (Exception e) {
            logger.info("请求返回信息：{}，异常信息：{}", result,e);
        }
        return response;
    }

}
